pip and venv in TrueNAS Scale? Give me both!
One day I was cleaning up my family’s photo archive, and to do it more efficiently I wanted to run a Python script.
The script relies on some extra packages being installed via pip
. “That should be trivial”, I thought. Spoiler alert: it wasn’t.
For my data-hoarding nightmare server, I’m rocking TrueNAS Scale, and as it is based on Debian, it comes with Python pre-installed. However, Debian distribution of Python doesn’t include neither pip
nor venv
modules, as those are supposed to be installed separately.
Ok, so it should be as simple as…
sudo apt install python3-pip python3-venv
…right?
No. TrueNAS Scale developers blocked apt
, as one can be afraid that you will ruin the OS integrity. Fair enough, but quite unhelpful.
Fortunately, there is a solution: zipapps!
Now, you might have never heard of it, as in my experience zipapp is definitely not the most common thing to come across. In one sentence - zipapp is Python built-in way of distributing code in a single file (usually .pyz
), which can be a library, an application, a CLI tool, etc.
So, how will it help us?
virtualenv
(not to be confused with Python’s standard library module venv
) also has a
zipapp distribution!
What’s perhaps even more important, is that when you create a virtual environment with virtualenv
- it comes pre-installed with pip
.
Now, this is how to get it working:
# get version of your Python interpreter. Mine was 3.11
export PYTHON_VERSION=$(python -c 'import sys; print(str(sys.version_info[0])+"."+str(sys.version_info[1]))')
# download virtualenv zipapp
wget "https://bootstrap.pypa.io/virtualenv/$PYTHON_VERSION/virtualenv.pyz"
# use virtualenv zipapp to create a virtual environment
python virtualenv.pyz .venv
# activate the virtual environment
source .venv/bin/activate
# check that we have pip
pip -V
That’s it 🎉! You now have pip
and virtual environments working in TrueNAS Scale. If you want to learn more about zipapps, check out the official documentation
here.